<template>
    <div>
        <el-dialog
            :title="!form.id ? '新增' : '修改'"
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">
                <el-form ref="form" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="120px">
                    <el-form-item label="公司照片" prop="companyPic">
                        <el-input placeholder="" v-model="form.companyPic" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="公司名称" prop="companyName">
                        <el-input placeholder="" v-model="form.companyName" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="公司电话" prop="companyPhone">
                        <el-input placeholder="" v-model="form.companyPhone" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="姓名" prop="name">
                        <el-input placeholder="" v-model="form.name" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号" prop="phone">
                        <el-input placeholder="" v-model="form.phone" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="代理地区" prop="area">
                        <el-input placeholder="" v-model="form.area" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="详情地址" prop="address">
                        <el-input placeholder="" v-model="form.address" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="创建时间" prop="ctime">
                        <el-input placeholder="" v-model="form.ctime" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="更新时间" prop="utime">
                        <el-input placeholder="" v-model="form.utime" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <!--
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio-button :label="1">正常</el-radio-button>
                            <el-radio-button :label="0">锁定</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="薪资" prop="salary">
                        <el-input-number v-model="form.salary" :precision="2" :step="0.01" :min="0.00" style="width: 80%;"></el-input-number>
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="form.sort" :min="1" :max="10000" placeholder="排序"></el-input-number>
                    </el-form-item>
                    <el-form-item label="性别" prop="sex">
                        <el-select v-model="form.sex" placeholder="请选择">
                            <el-option label="男" value="男"></el-option>
                            <el-option label="女" value="女"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="生日" prop="birthday">
                        <el-date-picker
                            v-model="form.birthday"
                            type="date"
                            placeholder="选择日期"
                            value-format="yyyy-MM-dd"
                            style="width: 63%;">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="备注" prop="remarks">
                        <el-input v-model="form.remarks" type="textarea" :rows="4" placeholder="请输入内容" style="width: 80%;"></el-input>
                    </el-form-item>
                    -->

                </el-form>
            </div>


            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            submitLoading: false,

            form: {
                companyPic: null,
                companyName: null,
                companyPhone: null,
                name: null,
                phone: null,
                area: null,
                address: null,
                ctime: null,
                utime: null
            },
            rules: {
                companyPic: [
                    { required: true, message: '公司照片不能为空', trigger: 'blur' }
                ],
                companyName: [
                    { required: true, message: '公司名称不能为空', trigger: 'blur' }
                ],
                companyPhone: [
                    { required: true, message: '公司电话不能为空', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '手机号不能为空', trigger: 'blur' }
                ],
                area: [
                    { required: true, message: '代理地区不能为空', trigger: 'blur' }
                ],
                address: [
                    { required: true, message: '详情地址不能为空', trigger: 'blur' }
                ],
                ctime: [
                    { required: true, message: '创建时间不能为空', trigger: 'blur' }
                ],
                utime: [
                    { required: true, message: '更新时间不能为空', trigger: 'blur' }
                ],
            }
        }
    },

    methods: {
        init(id) {
            const refs = this.$refs
            // 情况表单数据
            this.form.id = id || null
            this.visible = true

            this.$nextTick(() => {
                refs['form'].resetFields()
                if(this.form.id) {
                    this.getInfo(this.form.id)
                }
            })
        },

        /**
         * 详细信息
         */
        async getInfo(id) {
            if (id) {
                const res = await this.$http({
                    url: '/mall/wmsagentinfo/info/' + id,
                    method: 'GET'
                })
                this.form = Object.assign({}, res.data)
            }
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            const refs = this.$refs
            this.submitLoading = true
            const valid = await refs['form'].validate().catch(() => {})
            if(valid) {
                const res = await this.$http({
                    url: `/mall/wmsagentinfo/${!this.form.id ? 'save' : 'update'}`,
                    method: 'POST',
                    data: this.form
                })
                if (this.ResultMessage(res)) {
                    this.$emit('refreshDataList')
                    this.submitLoading = false
                    this.visible = false
                } else {
                    this.submitLoading = false
                }
            } else {
                this.submitLoading = false
            }
        }

    }
};
</script>

<style lang="scss" scoped></style>
